package com.wngbms.concurrent.future;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
public class CompletableFutureDemo {

    public static void main(String[] args) {

        CompletableFuture<Void> f1 = CompletableFuture.runAsync(() -> {
            log.info("洗水壶");
            sleep(1L);
            log.info("烧开水");
            sleep(15L);
        });

        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            log.info("洗茶壶");
            sleep(1L);
            log.info("洗茶杯");
            sleep(1L);
            log.info("拿茶叶");
            sleep(1L);
            return "龙井";
        });

        CompletableFuture<String> f3 = f1.thenCombine(f2, (__, y) -> {
            log.info("{} {}", __, y);
            log.info("拿到茶叶:{}", y);
            log.info("泡茶");
            return "上茶:" + y;
        });

        log.info(f3.join());

    }

    public static void sleep(long l) {
        try {
            TimeUnit.SECONDS.sleep(l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
